﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint;
using Microsoft.VisualBasic;
using System.Globalization;

namespace NumberToTextField
{
    public class Numeric_StringArabic : NumbertoTextBase  

    {
        string[] Ones = new string[9];
        string[] Tens = new string[9];
        string[] hand = new string[9];
        string[] Over = new string[6];
        string Str_Nom = "";
        string Sp_Str = "";
        public string Unit = " ";
        
        //CultureInfo  locale ;

        //LanguageManager GetLanguageManager() 
        //{ 
        //LanguageManager LANGManager;
        //    if(locale == null )
        //     LANGManager = new LanguageManager(System.Threading.Thread.CurrentThread.CurrentUICulture);
        //    else
        //        LANGManager = new LanguageManager(locale);

        //    return LANGManager;
        //}
        public  string parse2(string Value)
        {

            LanguageManager LANGManager = GetLanguageManager();
            set_strings();
            Value=Value.Trim();
            Value = Value.PadLeft(12, '0');
            Str_Nom = "";
            int result = 0;
            int ac = 0;
            for (int i = 0; i < 12; i = i + 3)
            {
                result = Convert.ToInt32(Value.Substring(i, 3));

                if (result > 0)
                {
                    Sub_String(result);
                    if (Str_Nom != "")
                        Str_Nom = Str_Nom + LANGManager.GetLocalizedString("And");// " و ";
                    if ((ac < 3) && (Sp_Str == LANGManager.GetLocalizedString("One")))//" واحد"))
                        Sp_Str = "";
                    if ((result < 9) && (ac == 2))
                    {
                        if ((result > 2) && (result < 9) && (ac == 2))
                        {
                            Str_Nom = Str_Nom + Sp_Str + Over[5];
                        }
                        else
                        {
                            if ((result == 2) && (ac == 2))
                            {
                                Str_Nom = Str_Nom  + Over[4];
                            }
                            else
                            {
                                if ((result == 1) && (ac == 2))
                                {
                                    Str_Nom = Str_Nom + Sp_Str + Over[2];
                                }

                            }
                        }
                    }
                    else
                    {
                        Str_Nom = Str_Nom + Sp_Str + Over[ac];
                    }
                }
                ac = ac + 1;
            }
            return Str_Nom;
        }

        void Sub_String(int number)
        {

            LanguageManager LANGManager = GetLanguageManager();
            
            int Bagi = 0;
            //int res = 0;
            string Str = "";
            if ((number / 100) > 0)
                Sp_Str = hand[(number / 100) - 1];

            Bagi = number - (number / 100) * 100;
            if ((Bagi > 0) && (Sp_Str != ""))

                Sp_Str = Sp_Str + LANGManager.GetLocalizedString("And");// " و ";
            if ((Bagi >= 1) && (Bagi < 9))
                Sp_Str = Sp_Str + Ones[Bagi - 1];
            if (Bagi == 10)
                Sp_Str = Sp_Str + LANGManager.GetLocalizedString("Ten"); //" عشرة";
        
        



            if (Bagi == 11)
                Sp_Str = Sp_Str + LANGManager.GetLocalizedString("Eleven");// " احد عشرة";
            if (Bagi == 12)
                Sp_Str = Sp_Str + LANGManager.GetLocalizedString("Twelve");// " اثنتا عشرة";
            if ((Bagi >= 13) && (Bagi <= 19))
                Sp_Str = Sp_Str + Ones[Bagi - 11] + LANGManager.GetLocalizedString("Ten1");// " عشر";
            if ((Bagi >= 20) && (Bagi <= 99))
            {
                int one = Bagi % 10;
                int Ten = Bagi / 10;
                if (one > 0)
                    Str = Ones[one - 1] + LANGManager.GetLocalizedString("And");// " و ";
                Str = Str + Tens[Ten - 1];
                Sp_Str = Sp_Str + Str;
            }

        }

        void set_strings()
        {
            LanguageManager LANGManager = GetLanguageManager();

            Ones[0] = LANGManager.GetLocalizedString("One");/* " واحد";*/ Ones[1] = LANGManager.GetLocalizedString("Two");// " اثنان";
            //Ones[2] = " ثلاثة"; Ones[3] = " أربعة"; Ones[4] = " خمسة";
            Ones[2] = LANGManager.GetLocalizedString("Three"); Ones[3] = LANGManager.GetLocalizedString("Four"); Ones[4] = LANGManager.GetLocalizedString("Five");
            //Ones[5] = " ستة"; Ones[6] = " سبعة"; Ones[7] = " ثمانية"; Ones[8] = " تسعة";
            Ones[5] = LANGManager.GetLocalizedString("Six"); Ones[6] = LANGManager.GetLocalizedString("Seven"); Ones[7] = LANGManager.GetLocalizedString("Eight"); Ones[8] = LANGManager.GetLocalizedString("Nine");
            //Tens[0] = " عشرة"; Tens[1] = " عشرون";
            Tens[0] = LANGManager.GetLocalizedString("Ten"); Tens[1] = LANGManager.GetLocalizedString("Twenty");
            //Tens[2] = " ثلاثون"; Tens[3] = " أربعون"; Tens[4] = " خمسون";
            Tens[2] = LANGManager.GetLocalizedString("Thirty"); Tens[3] = LANGManager.GetLocalizedString("Forty"); Tens[4] = LANGManager.GetLocalizedString("Fifty");

            //Tens[5] = " ستون"; Tens[6] = " سبعون"; Tens[7] = " ثمانون"; Tens[8] = " تسعون";
            Tens[5] = LANGManager.GetLocalizedString("Sixty"); Tens[6] = LANGManager.GetLocalizedString("Seventy"); Tens[7] = LANGManager.GetLocalizedString("Eighty"); Tens[8] = LANGManager.GetLocalizedString("Nighty");

            //hand[0] = " مائة"; hand[1] = " مائتان";
            //hand[2] = " ثلاثمائة"; hand[3] = " أربعمائة"; hand[4] = " خمسمائة";
            //hand[5] = " ستمائة"; hand[6] = " سبعمائة"; hand[7] = " ثمانمائة"; hand[8] = " تسعمائة";
            hand[0] = LANGManager.GetLocalizedString("Hundred"); hand[1] = LANGManager.GetLocalizedString("Twohundred");
            hand[2] = LANGManager.GetLocalizedString("Threehundred"); hand[3] = LANGManager.GetLocalizedString("Fourhundred"); hand[4] = LANGManager.GetLocalizedString("Fivehundred");
            hand[5] = LANGManager.GetLocalizedString("Sixhundred"); hand[6] = LANGManager.GetLocalizedString("Sevenhundred"); hand[7] = LANGManager.GetLocalizedString("Eighthundred"); hand[8] = LANGManager.GetLocalizedString("Ninehundred");


            //Over[0] = " مليار"; Over[1] = " مليون"; Over[2] = " ألف"; Over[3] = " " + Unit.Trim(); Over[4] = " ألفان"; Over[5] = " ألاف";
            Over[0] = LANGManager.GetLocalizedString("Billion"); Over[1] = LANGManager.GetLocalizedString("Million"); Over[2] = LANGManager.GetLocalizedString("Thousand"); Over[3] = " " + Unit.Trim(); Over[4] = LANGManager.GetLocalizedString("twoThousand"); Over[5] = LANGManager.GetLocalizedString("Thousands"); 
        }
        
        public override string  parse(string Value)
        {
            LanguageManager LANGManager = GetLanguageManager();

            set_strings();
            Value = Value.Trim();
            Value = Value.PadLeft(12, '0');
            Str_Nom = "";
            int result = 0;
            int ac = 0;
            for (int i = 0; i < 12; i = i + 3)
            {
                result = Convert.ToInt32(Value.Substring(i, 3));

                if (result > 0)
                {
                    Sub_String(result);
                    if (Str_Nom != "")
                        Str_Nom = Str_Nom + LANGManager.GetLocalizedString("And");// " و ";
                    if ((ac < 3) && (Sp_Str == LANGManager.GetLocalizedString("One")))//" واحد"))
                        Sp_Str = "";
                    if ((result < 9) && (ac == 2))
                    {
                        if ((result > 2) && (result < 9) && (ac == 2))
                        {
                            Str_Nom = Str_Nom + Sp_Str + Over[5];
                        }
                        else
                        {
                            if ((result == 2) && (ac == 2))
                            {
                                Str_Nom = Str_Nom + Over[4];
                            }
                            else
                            {
                                if ((result == 1) && (ac == 2))
                                {
                                    Str_Nom = Str_Nom + Sp_Str + Over[2];
                                }

                            }
                        }
                    }
                    else
                    {
                        Str_Nom = Str_Nom + Sp_Str + Over[ac];
                    }
                }
                ac = ac + 1;
            }
            return Str_Nom;
            //return System.Threading.Thread.CurrentThread.CurrentUICulture.ToString();
        }



        //void INumbertoText.setLocale(CultureInfo Value)
        //{
        //    locale = Value; 
        //}
    }

}
